package com.thinkgsp.webapp.controller.rp;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.thinkgsp.jxc.model.Customer;
import com.thinkgsp.jxc.model.RpCustomerContact;

import com.thinkgsp.jxc.model.RpContact;
import com.thinkgsp.jxc.service.ContactBalanceService;
import com.thinkgsp.jxc.service.RpContactService;
import com.thinkgsp.webapp.controller.BaseController;
import com.thinkgsp.webapp.util.ExportExcel;
import com.thinkgsp.webapp.util.WebUtils;

/**
 * 客户应收应付往来控制器
 * @author evan
 *
 */
@Controller
@RequestMapping(value="/rp/cscontact")
public class RpCustomerContactController extends BaseController {

	@Autowired
	private RpContactService contactService;
	
	@Autowired
	private ContactBalanceService cbService;
	
	@RequestMapping(value="/contactList.do")
	@ResponseBody
	public Object getCustomerContactList(@RequestParam("begYear") Integer begYear,
											@RequestParam("begPeriod") Integer begPeriod,
											@RequestParam("endYear") Integer endYear,
											@RequestParam("endPeriod") Integer endPeriod,
											@RequestParam("custId") Integer custId,
											@RequestParam("start") int start,
											@RequestParam("limit") int limit) {
		if(custId != null && custId.intValue() == 0) {
			custId = null;
		} 
		return cbService.getCustomerContactList(begYear, begPeriod, endYear, endPeriod,custId,start,limit);
	}
	
	@RequestMapping(value="/contactDetail.do")
	@ResponseBody
	public Object getCustomerContactDetail(@RequestParam("begYear") Integer begYear,
											@RequestParam("begPeriod") Integer begPeriod,
											@RequestParam("endYear") Integer endYear,
											@RequestParam("endPeriod") Integer endPeriod,
											@RequestParam("custId") Integer custId,
											@RequestParam("includeCheckTag") Boolean includeCheckTag) {
		if(includeCheckTag) {
			return contactService.getRpContactList(begYear, begPeriod, endYear, endPeriod, custId);
		} else {
			return contactService.getRpContactListNotCheck(begYear, begPeriod, endYear, endPeriod, custId);
		}
	}
	
	//@RequestMapping(value="/contactDetailExcCheck.do")
	@ResponseBody
	@Deprecated
	public Object getCustomerContactDetailNotCheck(@RequestParam("begYear") Integer begYear,
											@RequestParam("begPeriod") Integer begPeriod,
											@RequestParam("endYear") Integer endYear,
											@RequestParam("endPeriod") Integer endPeriod,
											@RequestParam("custId") Integer custId) {
		
		return contactService.getRpContactListNotCheck(begYear, begPeriod, endYear, endPeriod, custId);
	}
	@RequestMapping(value = "/printContact.do", method = RequestMethod.GET)
	public ModelAndView printContact(@RequestParam("begYear") Integer begYear,
									@RequestParam("begPeriod") Integer begPeriod,
									@RequestParam("endYear") Integer endYear,
									@RequestParam("endPeriod") Integer endPeriod,
									@RequestParam("custId") Integer custId,
									@RequestParam("includeCheckTag") Boolean includeCheckTag) {
		
		List<RpContact> list = null;
		if(includeCheckTag) {
			list = contactService.getRpContactList(begYear, begPeriod, endYear, endPeriod, custId);
		} else {
			list = contactService.getRpContactListNotCheck(begYear, begPeriod, endYear, endPeriod, custId);
		}
		BigDecimal arecAmount = BigDecimal.ZERO;
		BigDecimal apayAmount = BigDecimal.ZERO;
		for(RpContact contact : list) {
			if(contact.getARecAmount() != null) {
				arecAmount = arecAmount.add(contact.getARecAmount());
			}
			if(contact.getRecAmount() != null) {
				arecAmount = arecAmount.subtract(contact.getRecAmount());
			}
			if(contact.getAPayAmount() != null) {
				apayAmount = apayAmount.add(contact.getAPayAmount());
			}
			if(contact.getPayAmount() != null) {
				apayAmount = apayAmount.subtract(contact.getPayAmount());
			}
		}
		Map<String,Object> parameterMap = new HashMap<String,Object>(); 
		parameterMap.put("datasource", list); 
		parameterMap.put("title", WebUtils.BILL_TITLE);
		Double bp = begYear + begPeriod/100.0;
		parameterMap.put("begPeriod", new BigDecimal(bp.toString()));
		Double ep = endYear + endPeriod/100.0;
		parameterMap.put("endPeriod", new BigDecimal(ep.toString()));
		Customer cust = list.get(0).getCustomer();
		parameterMap.put("custCode", cust.getCustomerCode());
		parameterMap.put("custName", cust.getCustomerName());
		parameterMap.put("arecAmount", arecAmount);
		parameterMap.put("apayAmount", apayAmount);
		parameterMap.put("remainAmount", arecAmount.subtract(apayAmount));
		loger.debug(parameterMap);
		ModelAndView mav = new ModelAndView("customerContactReport",parameterMap);
		return mav;
	}
	@RequestMapping("/getCustomerContactXLS.do")
    public void  getXLS(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		try{
			int begYear=Integer.parseInt(request.getParameter("begYear"));
			int begPeriod=Integer.parseInt(request.getParameter("begPeriod"));
			int endYear=Integer.parseInt(request.getParameter("endYear"));
			int endPeriod=Integer.parseInt(request.getParameter("endPeriod"));
			String cid=request.getParameter("custId");
			Integer custId=null;
			if(StringUtils.isNotEmpty(cid) && !"0".equals(cid)) {
				custId = Integer.valueOf(cid);
			} 
			
			response.setContentType("octets/stream");
	        response.addHeader("Content-Disposition","attachment;filename="+ new String(("往来对账导出").getBytes("GB2312"),"iso8859-1")+".xls");
	        
			List<RpCustomerContact> list = null;
		    list = cbService.getCustomerContactList(begYear, begPeriod, endYear, endPeriod, custId);

			OutputStream out = response.getOutputStream();
	        ExportExcel<RpCustomerContact> ex = new ExportExcel<RpCustomerContact>();
	        HSSFWorkbook book= ex.exportExcelByFieldAnnotation(list);
	        book.write(out);
			
		}catch(Exception e){
			loger.error(e);	
		}		 
	}
	
	@RequestMapping("/getContactDetailXLS.do")
	public void  getDetailXLS(HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		try{
			int begYear=Integer.parseInt(request.getParameter("begYear"));
			int begPeriod=Integer.parseInt(request.getParameter("begPeriod"));
			int endYear=Integer.parseInt(request.getParameter("endYear"));
			int endPeriod=Integer.parseInt(request.getParameter("endPeriod"));
			String cid=request.getParameter("custId");
			int custId=0;
			if(StringUtils.isNotEmpty(cid) && !"0".equals(cid)) {
				custId = Integer.parseInt(cid);
			} 
			boolean includeCheckTag=Boolean.parseBoolean(request.getParameter("includeCheckTag"));
			
			response.setContentType("octets/stream");
	        response.addHeader("Content-Disposition","attachment;filename="+ new String(("往来对账明细导出").getBytes("GB2312"),"iso8859-1")+".xls");
	        
			List<RpContact> list = null;
			if(includeCheckTag) {
				list= contactService.getRpContactList(begYear, begPeriod, endYear, endPeriod, custId);
			} else {
				list= contactService.getRpContactListNotCheck(begYear, begPeriod, endYear, endPeriod, custId);
			}

			OutputStream out = response.getOutputStream();
	        ExportExcel<RpContact> ex = new ExportExcel<RpContact>();
	        HSSFWorkbook book= ex.exportExcelByFieldAnnotation(list);
	        book.write(out);
			
		}catch(Exception e){
			loger.error(e);	
		}		 
	}
}
